package com.qdxwx.web.controller.book;

import com.qdxwx.common.json.AjaxResult;
import com.qdxwx.common.utils.TKConvert;
import com.qdxwx.common.utils.TKString;
import com.qdxwx.data.service.area.AreaService;
import com.qdxwx.data.service.book.BookRequireService;
import com.qdxwx.data.service.book.BookService;
import com.qdxwx.models.book.Book;
import com.qdxwx.models.book.BookRequire;
import com.qdxwx.web.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import jakarta.servlet.http.HttpServletRequest;

/**
 * 样书索取接口
 */
@Controller
@RequestMapping("/book")
public class BookRequireController extends BaseController {

    @Autowired
    private BookService bookService;
    @Autowired
    private BookRequireService bookRequireService;
    @Autowired
    private AreaService areaService;

    @RequestMapping("/require.html")
    public String require(HttpServletRequest request, ModelMap mmap) {
        int bookId = TKConvert.toInteger(request.getParameter("bookId"));
        if (bookId < 1) return toError404(request);
        Book book = bookService.get(bookId);
        if (book == null || book.getIsAvailable() < 1) return toError404(request);
        mmap.put("book", book);
        mmap.put("provinces", areaService.getChildren(0));
        return getViewPath("book/book-require");
    }

    @RequestMapping(value = "/require/save.json", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult save(HttpServletRequest request) {
        int bookId = TKConvert.toInteger(request.getParameter("bookId"));
        if (bookId < 1) return AjaxResult.error("参数bookId错误,bookId=" + bookId);
        Book book = bookService.get(bookId);
        if (book == null || book.getIsAvailable() < 1) return AjaxResult.error("找不到指定书籍，或该书籍已下架");
        String schoolName = TKString.trim(request.getParameter("schoolName"));
        if (TKString.isEmpty(schoolName)) return AjaxResult.error("请输入学校名称");
        String position = TKString.trim(request.getParameter("position"));
        if (TKString.isEmpty(position)) return AjaxResult.error("请输入校内职务");
        String address = TKString.trim(request.getParameter("address"));
        if (TKString.isEmpty(address)) {
            return AjaxResult.error("请输入详细地址");
        }
        String contacts = TKString.trim(request.getParameter("contacts"));
        if (TKString.isEmpty(contacts)) return AjaxResult.error("请输入联系人名称");
        String mobile = TKString.trim(request.getParameter("mobile"));
        if (!TKString.isMobile(mobile)) return AjaxResult.error("请输入正确的手机号码");
        BookRequire bookRequire = new BookRequire();
        bookRequire.setBookId(bookId);
        bookRequire.setContacts(contacts);
        bookRequire.setMobile(mobile);
        bookRequire.setSchoolName(schoolName);
        bookRequire.setStatus(0);
        int count = bookRequireService.count(bookRequire);
        if (count > 0) return AjaxResult.error("您已经提交过申请了，无需再次提交");
        bookRequire.setAreaId(TKConvert.toInteger(request.getParameter("areaId")));
        bookRequire.setAddress(address);
        bookRequire.setPosition(position);
        int row = bookRequireService.insert(bookRequire);
        return row > 0 ? AjaxResult.success() : AjaxResult.error();
    }

}
